package main

import (
	"fmt"
	"github.com/juju/ratelimit"
	"time"
)

var bucketMap map[int64]*ratelimit.Bucket

func newBucket() *ratelimit.Bucket{
	//rate表示每秒放多少个令牌
	//capacity表示桶的最大容量
	bucket := ratelimit.NewBucketWithRateAndClock(2, 5, nil)
	return bucket
}

func main(){

	b := newBucket()
	for i := 0; i<1000; i++{
		before := b.Available()
		if b.TakeAvailable(1) != 0{	//取到了令牌
			fmt.Println("获取到令牌index ", i+1, "前后数量-->前：", before, "后：", b.Available())
		}else{
			fmt.Println("未取到令牌，拒绝 ", i+1)
		}
		time.Sleep(1*time.Second/10)
	}

}